﻿using System;
using System.Reflection;

namespace Caramel.Entities.Testing
{
	public class ReferenceProperty<T, TProperty> : Property<T, TProperty> where T : class, IEntity, new()
	{
		public ReferenceProperty(PropertyInfo member, Func<TProperty, TProperty, bool> comparer)
			: base(member, comparer)
		{
		}

		public override void CheckValue(T expected, T actual)
		{
			var expectedReference = this.GetValue(expected);
			var actualReference = this.GetValue(actual);

			if (!((IEntity)expectedReference).ID.Equals(((IEntity)actualReference).ID))
			{
				throw new ApplicationException(String.Format("For {0} expected reference with ID {1} but got reference with ID {2}.", this.Name, ((IEntity)expectedReference).ID, ((IEntity)actualReference).ID));
			}
		}
	}
}
